package com.GlobalPaint.app.Adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.MyFriendBean;
import com.GlobalPaint.app.ui.App;
import com.GlobalPaint.app.ui.Mine.Personal.PersonalZhanshi;
import com.GlobalPaint.app.utils.Constants;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/18 0018.
 */

public class FriendsActivityAdapter extends XRecyclerView.Adapter<FriendsActivityAdapter.ViewHoler>{
    private List<MyFriendBean.DataBean> bean;
    public List list_temp=new ArrayList();
    public boolean[] flag;
    public FriendsActivityAdapter(List<MyFriendBean.DataBean> list) {
        bean=list;
        if (list!=null&&list.size()>0){
            flag= new boolean[list.size()+1];
        }
    }

    @Override
    public FriendsActivityAdapter.ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_friends_item, parent, false);
        FriendsActivityAdapter.ViewHoler holder = new FriendsActivityAdapter.ViewHoler(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final FriendsActivityAdapter.ViewHoler holder, final int position) {
        String name=bean.get(position).getUserName()==
                null?"":bean.get(position).getUserName();
        holder.tv_name.setText(name);
        String nick=bean.get(position).getNick()==
                null?"":bean.get(position).getNick();
        String Url= bean.get(position).getAvatar()== null?"wu":
                bean.get(position).getAvatar();
        if(Url.equals("wu")){
            Picasso.with(App.context).load(R.drawable.touxiang).into(holder.imge);
        }else {
            Picasso.with(App.context).load(Constants.JASON_SERVICE_URL_TOUXIANG+Url)
                    .placeholder(R.drawable.touxiang).into(holder.imge);
        }
        holder.tv_nick.setText(nick);
  /*      holder.mview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    Intent intent = new Intent(view.getContext(), ChatActivity.class);
                    intent.putExtra("from", bean.get(position).getJid());
                    view.getContext().startActivity(intent);
            }
        });*/
        holder.imge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), PersonalZhanshi.class);
                intent.putExtra("userid",bean.get(position).getUserId());
                view.getContext().startActivity(intent);
            }
        });
        holder.checkbox.setOnCheckedChangeListener(null);//先设置一次CheckBox的选中监听器，传入参数null
        holder.checkbox.setChecked(flag[position]);//用数组中的值设置CheckBox的选中状态
        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked)
                {
                    flag[position] = isChecked;
                    //list_temp.add(String.valueOf(position));
                    list_temp.add(String.valueOf(bean.get(position).getUserId()));
                }
                else
                {
                    flag[position] = false;
                    list_temp.remove(String.valueOf(bean.get(position).getUserId()));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return bean.size();
    }

    public class ViewHoler extends RecyclerView.ViewHolder {

        private final TextView tv_name;
        private final TextView tv_nick;
        private final ImageView imge;
        private final CheckBox checkbox;

        View mview;

        public ViewHoler(View itemView) {
            super(itemView);
            mview = itemView;
            tv_name = (TextView)itemView.findViewById(R.id.tv_name);
            tv_nick = (TextView)itemView.findViewById(R.id.tv_nick);
            imge = (ImageView)itemView.findViewById(R.id.imge);
            checkbox = (CheckBox)itemView. findViewById(R.id.checkbox);
        }
    }
}
